import { reqGoodsInfo,reqAddOrUpdateShopCart } from "@/api";
//uuid 调用就会生成一个随机字符串(永久性的)
import {getUUID} from '@/utils/uuid_token'
const state = {
    goodInfo:{},
    //弄个游客临时身份
    uuid_token:getUUID()
    //
};
const mutations = {
    GETGOODINFO(state,goodInfo){
      state.goodInfo = goodInfo;
    }
};
const actions = {
     //读取产品信息
   async  getGoodInfo({commit},skuid){
        let res = await reqGoodsInfo(skuid);
        if(res.code == 200){
          commit("GETGOODINFO",res.data);
        }
     },
     //添加购物车
   async  addOrUpdateShopCart({commit},{skuId,skuNum}){
      let res = await reqAddOrUpdateShopCart(skuId,skuNum);
      //因为服务器并没有返回其余数据,因此不需要三连环存储数据
      //但服务器写入数据是否成功不确定,返回一个值来作为提示是否成功
      if(res.code == 200){
        return "加入成功"
      }else{
        return Promise.reject(new Error('fail'));
      }
     }
}
//简化数据
const getters = {
  //路径导航简化
  categoryView(state){
   return state.goodInfo.categoryView || {};
  },
  //产品简化
  skuInfo(state){
    return state.goodInfo.skuInfo||{};
  },
  //产品售卖属性的简化
  spuSaleAttrList(state){
    return state.goodInfo.spuSaleAttrList||[]
  }

};
export default {
  namespaced:true,
    state,
    mutations,
    actions,
    getters,
}